<template>
	<view class="container">
		<!-- Navigation Bar (Based on the visible bar in the screenshot) -->
		<Header :isHomepage="true"></Header>

		

		<!-- Middle Section Title -->
		<view class="middle-title-area">
			<text class="middle-title">快速建立网上纪念馆</text>
		</view>

		<!-- Main Content - Form Sections -->
		<view class="main-content">

			<!-- Section 1: 填写纪念馆资料 -->
			<view class="form-section">
				<view class="section-title">填写纪念馆资料</view>

				<view class="form-item">
					<text class="form-label">逝者姓名:</text>
					<view class="input-wrapper">
						<input class="form-input" type="text" placeholder="逝者姓名" v-model="formData.name"/>
					</view>
				</view>
				<view class="form-item">
					<text class="form-label">逝者照片:</text>
					<view class="form-value" style="border: none;">
						<upload-image @success="getUpLoadImage"></upload-image>
					</view>
				</view>
			</view>

			<view class="section-divider"></view>

			<view class="form-section" v-if="!isLogin">
				<view class="section-title">设置管理账号</view>
				<view class="form-section">
					<view class="form-item">
						<text class="form-label">手机号码:</text>
						<view class="input-wrapper">
							<input class="form-input" type="number" placeholder="输入手机号" v-model="account"/>
							<text class="input-suffix-text">同时用作登录账号</text>
						</view>
					</view>
					<view class="form-item">
						<text class="form-label">登录密码:</text>
						<view class="input-wrapper">
							<input class="form-input" type="password" placeholder="输入登录密码" v-model="password"/>
							<text class="input-suffix-text password-hint">6至20位</text>
						</view>
					</view>
					<view class="form-item">
						<text class="form-label">确认密码:</text>
						<view class="input-wrapper">
							<input class="form-input" type="password" placeholder="再次输入登录密码" v-model="password_confirm"/>
							<text class="input-suffix-text password-hint">6至20位</text>
						</view>
					</view>
					<view class="form-item">
						<text class="form-label">昵称或姓名:</text>
						<view class="input-wrapper">
							<input class="form-input" type="text" placeholder="请输入昵称" v-model="nickname"/>
						</view>
					</view>
					<view class="form-item">
						<text class="form-label">真实姓名:</text>
						<view class="input-wrapper">
							<input class="form-input" type="text" placeholder="请输入真实姓名" v-model="real_name"/>
						</view>
					</view>
					<view class="form-item">
						<text class="form-label">性别:</text>
						<view class="input-wrapper" style="border: none;">
							<van-radio-group v-model="sex" direction="horizontal">
							  <van-radio :name="0">保密</van-radio>
							  <van-radio :name="1">男</van-radio>
							  <van-radio :name="2">女</van-radio>
							</van-radio-group>
						</view>
					</view>
					<view class="form-item">
						<text class="form-label">身份证证件号:</text>
						<view class="input-wrapper">
							<input class="form-input" type="text" placeholder="请输入身份证证件号" v-model="id_card"/>
						</view>
					</view>
					<view class="form-item">
						<text class="form-label">邮箱地址:</text>
						<view class="input-wrapper">
							<input class="form-input" type="text" placeholder="请输入邮箱地址" v-model="email"/>
						</view>
					</view>
					<view class="form-item">
						<text class="form-label">联系地址:</text>
						<view class="input-wrapper">
							<input class="form-input" type="text" placeholder="请输入联系地址" v-model="address"/>
						</view>
					</view>
					<view class="form-item">
						<text class="form-label">邮政编码:</text>
						<view class="input-wrapper">
							<input class="form-input" type="text" placeholder="请输入邮政编码" v-model="post_code"/>
						</view>
					</view>
					<button class="submit-btn" @click="refreshPage">提交注册</button>
				
					<view class="terms-checkbox">
						<van-checkbox v-model="checked"><text class="terms-text">阅读<text class="terms-link">《今生永恒网服务条款》</text>提交注册表示已同意。</text></van-checkbox>
					</view>
				</view>
			</view>
			<button class="submit-btn" @click="submit">提交</button>
			<view class="terms-checkbox">
				<uni-data-checkbox multiple v-model="agreement"></uni-data-checkbox>
				<text class="terms-text">阅读<text class="terms-link">《今生永恒网服务条款》</text>提交注册表示已同意。</text>
			</view>

		</view>

	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import { onLoad } from '@dcloudio/uni-app'
	import Header from '@/components/header/index.vue'
	import { getToken } from '../../utils/token';
	import UploadImage from '@/components/upLoadImage/index.vue'; // 假设你把上面的代码保存在 components/UploadImage.vue
	import { my_mufast_add } from '@/api/graveyard'
	import { register, login } from '@/api/user.js'
	const isLogin = ref(false)
	const agreement = ref(true)
	
	const userInfo = ref()

	const account = ref('')
	const password = ref('')
	const password_confirm = ref('')
	const nickname = ref('')
	const real_name = ref('')
	const sex = ref(0)
	const id_card = ref('')
	const email = ref('')
	const address = ref('')
	const post_code = ref('')

	const formData = ref({
		name: '',
		avatar: ''
	})


	// 提交
	const submit = async () => {
		if(!getToken()) {
			if (!agreement.value) {
				return
			}
			if(!checked.value){
				uni.showToast({
					title: '请阅读并同意服务条款',
					icon: 'none',
					duration: 2000
				})
				return false
			}
			if(account.value == ''){
				uni.showToast({
					title: '请填写手机号',
					icon: 'none',
					duration: 2000
				})
				return false
			}
			if(password.value == ''){
				uni.showToast({
					title: '请填写密码',
					icon: 'none',
					duration: 2000
				})
				return false
			}
			if(password_confirm.value == ''){
				uni.showToast({
					title: '请输入确认密码',
					icon: 'none',
					duration: 2000
				})
				return false
			}
			if(password.value !== password_confirm.value){
				uni.showToast({
					title: '两次密码不一样',
					icon: 'none',
					duration: 2000
				})
				return false
			}
			if(nickname.value == ''){
				uni.showToast({
					title: '请填写昵称',
					icon: 'none',
					duration: 2000
				})
				return false
			}
			if(real_name.value == ''){
				uni.showToast({
					title: '请填写真实姓名',
					icon: 'none',
					duration: 2000
				})
				return false
			}
			if(id_card.value == ''){
				uni.showToast({
					title: '请填写身份证证件号',
					icon: 'none',
					duration: 2000
				})
				return false
			}
			let obj = {
				account: account.value,
				password: password.value,
				password_confirm: password_confirm.value,
				nickname: nickname.value,
				real_name: real_name.value,
				sex: sex.value,
				id_card: id_card.value,
				email: email.value,
				address: address.value,
				post_code: post_code.value,
			}
			await register(obj).then(async res=>{
				console.log(res)
				if(res.code == 1){
					uni.showToast({
						title: '注册成功',
						icon: 'none',
						duration: 2000
					})
					let query = {
						account: account.value.trim(),
						password: password.value,
						terminal: 3
					}
					await login(query).then(res=>{
						console.log(res)
						if(res.code == 1){
							setToken(res.data.token)
							uni.setStorageSync('userInfo', res.data)
							userInfo.value = res.data
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1000
							})
						}
					}).catch(error=>{
						console.log(error)
					})
				}
			}).catch(error=>{
				console.log(error)
			})
			
		}
		
		if(formData.value.name == ''){
			uni.showToast({
				title: '请填写逝者姓名',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if(formData.value.avatar == ''){
			uni.showToast({
				title: '请上传照片',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		let data = { ...formData.value }
		
		let params = {
			type: 1,
			user_id: userInfo.id, 
			is_double: 0,
		}
		await my_mufast_add({...data, ...params}).then(res=>{
			console.log(res)
			if(res.code == 1){
				uni.showToast({
					title: '添加成功',
					icon: 'none',
					duration: 1000
				})
				uni.navigateTo({
					url: '/pages/graveyardManage/manage/index'
				})
			}
		}).catch(error=>{
			console.log(error)
		})
	}
	
	
	
	// 获取上传数据
	const getUpLoadImage = (item) => {
		if(item != null && item != undefined && item != ''){
			formData.value.avatar = item
		}
	}
	
	onLoad(async () => {
		if(getToken() != '' && getToken()){
			isLogin.value = true
			userInfo.value = uni.getStorageSync('userInfo')
		}else{
			isLogin.value = false
		}
	})
</script>

<style scoped lang="scss">
	/* Scoped styles for this page */
	/* Reusing styles from the previous page where applicable */
	.container {
		max-width: 750rpx;
		margin: 0 auto;
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		background-color: #f0f0f0;
		/* Lighter gray background matching the image */
		padding-bottom: 50rpx;
		/* Add some padding at the bottom */
	}

	/* Navigation Bar Styles - Copied from previous, adjusted if necessary based on placement */
	/* Assuming this is the top bar based on the second screenshot */
	.nav-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 88rpx;
		padding: 0 20rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #eee;
	}

	.nav-left {
		display: flex;
		align-items: center;
	}

	.nav-icon {
		font-size: 40rpx;
		/* Adjust icon size */
		color: #333;
		margin-right: 30rpx;
		/* Spacing between icons */
	}

	.nav-right {
		display: flex;
		align-items: center;
	}

	.nav-link {
		font-size: 28rpx;
		color: #333;
		margin-left: 40rpx;
		/* Spacing between links */
	}

	.nav-link.active {
		color: #ff9800;
		/* Orange color for active/selected link */
		font-weight: bold;
	}

	/* Banner Styles - Copied from previous */
	.banner {
		width: 100%;
		height: 300rpx;
		/* Adjust height as needed */
		position: relative;
		overflow: hidden;
	}

	.banner-bg {
		width: 100%;
		height: 100%;
		position: absolute;
		top: 0;
		left: 0;
	}

	.banner-overlay {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 30rpx 40rpx;
		/* Adjust padding */
		color: #fff;
		/* Text color on banner */
		text-shadow: 1rpx 1rpx 2rpx rgba(0, 0, 0, 0.5);
	}

	.banner-title-area {
		display: flex;
		align-items: flex-end;
	}

	.banner-main-title {
		font-size: 50rpx;
		/* Adjust font size */
		font-weight: bold;
		margin-right: 20rpx;
	}

	.banner-sub-area {
		display: flex;
		flex-direction: column;
		font-size: 24rpx;
		/* Adjust font size */
	}

	.banner-url {
		font-size: 20rpx;
		/* Adjust font size */
	}

	.banner-slogan-area {
		display: flex;
		justify-content: flex-end;
		/* Align to the right */
		font-size: 36rpx;
		/* Adjust font size */
	}

	.banner-slogan {
		margin-left: 30rpx;
		/* Spacing between slogans */
		font-weight: bold;
	}

	/* Middle Section Title Styles */
	.middle-title-area {
		text-align: center;
		padding: 30rpx 0;
		/* Add vertical padding */
		background-color: #fff;
		/* White background for this section */
		margin-top: 20rpx;
		/* Space below banner */
	}

	.middle-title {
		font-size: 28rpx;
		color: #333;
		font-weight: bold;
	}


	/* Main Content Styles - Similar to previous, but adjusted margins/padding */
	.main-content {
		padding: 30rpx;
		background-color: #fff;
		margin: 20rpx 20rpx 0 20rpx;
		/* Side margins and top margin */
		border-radius: 10rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
		/* Subtle shadow */
	}

	/* Form Section Title Styles */
	.section-title {
		font-size: 28rpx;
		color: #4CAF50;
		/* Green color */
		font-weight: bold;
		padding-left: 20rpx;
		/* Space for the border */
		border-left: 8rpx solid #4CAF50;
		/* Green border on the left */
		margin-bottom: 40rpx;
		/* Space below the title */
	}

	/* Form Styles - Copied from previous, with additions for date hints and picker */
	.form-section {
		margin-bottom: 40rpx;
	}

	.form-item {
		margin-bottom: 30rpx;
		position: relative;
		/* Needed for absolute positioning of hints */
		
	}
	.form-item-left{
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.form-item-left-label{
			font-size: 28rpx;
			color: #333;
			margin-bottom: 10rpx;
			display: block;
		}
		.form-item-left-tips{
			font-size: 24rpx;
			color: #666;
			margin-bottom: 10rpx;
			display: block;
		}
	}
	.form-label {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 10rpx;
		display: block;
	}

	.input-wrapper {
		position: relative;
		display: flex;
		align-items: center;
		border: 1rpx solid #ddd;
		border-radius: 5rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
		height: 80rpx;
		width: calc(100%);
	}

	.form-input {
		
		flex: 1;
		height: 100%;
		font-size: 28rpx;
		color: #333;
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
		padding: 0;
		/* Ensure no extra padding */
		background-color: transparent;
		/* Ensure background is transparent for wrapper border */
	}

	.form-input::placeholder {
		color: #ccc;
	}

	/* Specific style for the date input hint text */
	.input-hint {
		position: absolute;
		right: 20rpx;
		/* Position relative to the input wrapper */
		top: -25rpx;
		/* Position above the input wrapper, adjust as needed */
		font-size: 22rpx;
		/* Smaller font size */
		color: #999;
		pointer-events: none;
		background-color: #fff;
		/* Background to cover potential content below */
		padding: 0 5rpx;
		/* Small padding */
		z-index: 1;
		/* Ensure it's above the input border */
	}

	/* Adjust position for icon suffix if present */
	.input-suffix-text {
		position: absolute;
		right: 20rpx;
		/* Adjust position */
		font-size: 24rpx;
		color: #999;
		pointer-events: none;
	}

	.input-icon {
		position: absolute;
		right: 20rpx;
		/* Position icons */
		font-size: 30rpx;
		/* Adjust icon size */
		color: #ccc;
		/* Icon color */
		pointer-events: none;
	}

	/* Adjust icon position if there's suffix text */
	.input-icon.right {
		right: 50rpx;
		/* Position icon when suffix text is present */
	}

	.password-hint,
	.name-hint {
		right: 50rpx;
		/* Position hint text next to icon */
	}


	/* Verify Code Item - Copied from previous */
	.verify-code-item {
		display: flex;
		align-items: center;
	}

	.code-input-wrapper {
		flex: 1;
		border: 1rpx solid #ddd;
		border-radius: 5rpx;
		padding: 0 20rpx;
		height: 80rpx;
		margin-right: 20rpx;
	}

	.code-input {
		border: none;
		padding: 0;
	}

	.get-code-btn {
		height: 80rpx;
		line-height: 80rpx;
		font-size: 28rpx;
		color: #fff;
		background-color: #ff9800;
		border-radius: 5rpx;
		padding: 0 30rpx;
		border: none;
		margin: 0;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	/* Picker Styles - Make it look like an input */
	.picker-wrapper {
		width: calc(100%);
		box-sizing: border-box;
		padding: 0 20rpx;
		/* Match input padding */
		/* Border, height etc. are already handled by .input-wrapper */
	}
	.picker-wrapper-box{
		width: 100%;
	}
	.picker-text {
		width: 100%;
		flex: 1;
		/* Allow text to grow */
		height: 100%;
		/* Match wrapper height */
		line-height: 80rpx;
		/* Center text vertically */
		font-size: 28rpx;
		color: #333;
		/* Match input text color */
		/* Adjust color if placeholder is shown */
		text-align: left;
		/* Align text left */
	}

	.picker-text[data-v-xxxx] {
		/* You might need to adjust the data-v- scope hash */
		color: #ccc;
		/* Placeholder color if no value selected */
	}

	/* A more robust way to handle placeholder color for picker text */
	.picker-text:empty::before {
		content: '-------请选择-------';
		/* Placeholder content */
		color: #ccc;
		/* Placeholder color */
	}


	/* Divider Style */
	.section-divider {
		height: 1rpx;
		background-color: #eee;
		margin: 40rpx 0;
		/* Space above and below the divider */
	}


	/* Submit Button Style - Text is different, but style is similar */
	.submit-btn {
		height: 90rpx;
		line-height: 90rpx;
		font-size: 32rpx;
		color: #fff;
		background-color: #ff9800;
		/* Orange color */
		border-radius: 8rpx;
		/* More rounded corners */
		text-align: center;
		margin-top: 40rpx;
		/* Space above the button */
		border: none;
		margin-bottom: 30rpx;
	}

	/* Terms Checkbox - Copied from previous */
	.terms-checkbox {
		display: flex;
		align-items: flex-start;
		font-size: 26rpx;
		color: #666;
	}

	.terms-text {
		flex: 1;
	}

	.terms-link {
		color: #007aff;
		/* Blue color for link */
		text-decoration: underline;
	}


	

	/* No Floating Button in this specific screenshot */
</style>